/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-08-26 10:42:23
 * @LastEditors: TT-hzy
 * @LastEditTime: 2024-08-11 13:32:43
 */

import 'package:flutter/material.dart';
import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/models/image_normal_model.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

import '../config/note_login_config.dart';
import '../tools/login_module_tool.dart';
import 'login_agreement_widget.dart';
import 'login_compontents.dart';

class LoginChildWidget extends StatefulWidget {
  const LoginChildWidget({
    super.key,
    required this.config,
    required this.tapCallback,
  });
  final Function(ImageNormalModel? imageNormalModel) tapCallback;
  final NoteLoginConfig config;
  @override
  State<StatefulWidget> createState() {
    return LoginChildWidgetState();
  }
}

class LoginChildWidgetState extends CommonStateWidget<LoginChildWidget> {
  late NoteLoginConfig _noteLoginConfig;

  ImageNormalModel? imageNormalModel;
  bool isAgree = false;
  GlobalKey<LoginAgreementState> agreeKey = GlobalKey();
  @override
  void initState() {
    _noteLoginConfig = widget.config;
    imageNormalModel = _noteLoginConfig.imageNormalModel;
    super.initState();
  }

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        createTopInfoWidget(),
        createBottomInfoWidget(),
      ],
    );
    body = Stack(
      children: [
        body,
        HzyAppBarGenerator.customAppBarWidget(
          context: context,
          icon: Icons.close,
        ),
      ],
    );
    return body;
  }

  /// 创建用户信息界面
  createTopInfoWidget() {
    Widget body = Column(
      children: [
        Gaps.configvbox(
          height: kToolbarHeight + 30,
        ),
        ...configHeaderWidget(),
        ...configNickNameWidget(),
        ...configInputSWidget(),
      ],
    );

    body = Padding(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: body,
    );

    return body;
  }

  /// 创建头像
  List<Widget> configHeaderWidget() {
    List<Widget> list = _noteLoginConfig.headerType == 1
        ? [
            NormalUserInfoWidget.createUserInfoHeaderImgWidget(
              imageNormalModel: imageNormalModel,
              width: 70,
              radius: 10,
              fontSize: 20,
              type: 2,
              isCanTap: _noteLoginConfig.inputStype == 3,
              tapCallback: () {
                tapChangeHeaderImage();
              },
            ),
            Gaps.vGap20,
          ]
        : [];
    return list;
  }

  /// 创建昵称
  List<Widget> configNickNameWidget() {
    Widget body = _noteLoginConfig.nickNameType == 3
        ? Container()
        : _noteLoginConfig.nickNameType == 1
            ? NormalUserInfoWidget.createUserNameWidget(
                name: _noteLoginConfig.nickName,
                style: FontConfig.fontBold16Black,
              )
            : appSlogWidget();
    List<Widget> list = [
      body,
      _noteLoginConfig.nickNameType == 2 ? Gaps.vGap40 : Gaps.vGap20,
    ];
    return list;
  }

  /// 创建输入框内容
  List<Widget> configInputSWidget() {
    switch (_noteLoginConfig.inputStype) {
      case 1:
        _noteLoginConfig.list = [
          _noteLoginConfig.passWordInputModel,
        ];
        break;
      case 2:
        _noteLoginConfig.list = [
          _noteLoginConfig.accountInputModel,
          _noteLoginConfig.passWordInputModel,
        ];
        break;
      case 3:
        _noteLoginConfig.list = [
          _noteLoginConfig.accountInputModel,
          _noteLoginConfig.nickNameInputModel,
          _noteLoginConfig.passWordInputModel,
        ];
        break;
      default:
    }
    List<Widget> body = [];

    for (var i = 0; i < _noteLoginConfig.list.length; i++) {
      InputModel inputModel = _noteLoginConfig.list[i];
      if (i == 0) {
        body.add(
          HzyNormalTools.configLineSpace(
            height: 0.5,
            color: getColorFromHex("#e5e5e5"),
          ),
        );
      }
      body.add(
        NormalInputWidget(inputModel: inputModel),
      );
      body.add(
        HzyNormalTools.configLineSpace(
          height: 0.5,
          color: getColorFromHex("#e5e5e5"),
        ),
      );
    }

    return body;
  }

  /// 创建底部视图
  createBottomInfoWidget() {
    Widget body = LoginCompontentS.configLoginBtnWidget(
      tapCallback: () {
        tapBottonBtn();
      },
      text: _noteLoginConfig.text,
    );

    body = Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        body,
        _noteLoginConfig.bottomType == 3 ? Gaps.vGap10 : Gaps.vGap6,
        LoginCompontentS.configBottomOtherLogin(
          type: _noteLoginConfig.bottomType,
          account: _noteLoginConfig.account,
        ),
        _noteLoginConfig.bottomType == 3
            ? LoginAgreementWidget(
                key: agreeKey,
                tapBoolCallback: (yorn) {
                  isAgree = yorn;
                },
              )
            : Container(),
      ],
    );
    body = Padding(
      padding: EdgeInsets.symmetric(
        horizontal: 20.w,
        vertical: 10.w,
      ),
      child: body,
    );
    return body;
  }

  tapChangeHeaderImage() async {
    var res = await configChangeHeaderImage(
      type: 1,
    );
    if (res != null) {
      imageNormalModel = res["info"];

      configSetState(() {});
    }
  }

  /// 点击确认按钮
  tapBottonBtn() async {
    if (!isAgree && _noteLoginConfig.bottomType == 3) {
      isAgree = await LoginModuleTool.configAgreeIsShowPopWidget(
        yorn: isAgree,
      );
      agreeKey.currentState?.configIsTapAgreement(
        yorn: isAgree,
      );
    } else {
      bool yorn = configListInputBoxContentJudgment(
        list: _noteLoginConfig.list,
      );
      if (yorn) {
        widget.tapCallback(imageNormalModel);
      }
    }
  }
}
